//
//  FindViewController.m
//  Everest
//
//  Created by 李其昌 on 14/12/17.
//  Copyright (c) 2014年 ThreeL. All rights reserved.
//


#import "SearchViewController.h"
#import "MMDrawerBarButtonItem.h"
#import "FindViewController.h"
#import "EverestAPIUrl.h"
#import "NetHandler.h"
#import "FindImageScroll.h"
#import "EScrollerView.h"
#import "Classify.h"
#import "FindClassifyViewController.h"
#import "MyNavigationView.h"
#import "FindTodayFocus.h"
#import "FindMayBeLikeUser.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "FindHotPresenterViewController.h"
#import "HotPresenterHomepageController.h"
#import "Recommend.h"
#import "MJRefresh.h"
#import "TittleCell.h"
#import "UIViewController+MMDrawerController.h"
#import "FindClassifyDetailViewController.h"
#import "AlbumViewController.h"
#import "ClassifyDetail.h"
#import "FocusMoreViewController.h"
#import "FoThreeViewController.h"
#import "PlayViewController.h"
#import "Album.h"
#import "ComprehensiveAlbum.h"
#import "CategoriesNew.h"
#import "DataBaseHandler.h"
#import "UIColor+AddColor.h"
#import "SDImageCache.h"
#import "FilesDownManage.h"
#import "UIViewController+MMDrawerController.h"

#define kTodayFocusTag 100
#define kMayBeLikeUserTag 0
#define kReuse @"reuse"
#define kCollectionReuse @"collectionReuse"


// 通知中心的标识名和cell的重用标识都要写成全局

@interface FindViewController () <UITableViewDataSource, UITableViewDelegate, EScrollerViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UISearchBarDelegate>
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UITextField *searchField;
@property (nonatomic, retain) UITableView *mainTableView;
@property (nonatomic, retain) EScrollerView *smallScrollView;
@property (nonatomic, retain) NSMutableArray *imageUrlArr;
@property (nonatomic, retain) NSMutableArray *buttonArr;
@property (nonatomic, retain) NSMutableArray *tittleArr;
@property (nonatomic, retain) NSMutableArray *todayFocusArr;
@property (nonatomic, retain) NSMutableArray *maybeLikeUserArr;
@property (nonatomic, retain) NSMutableArray *moduleTitleArr;
@property (nonatomic, retain) NSMutableArray *recommendArr;
@property (nonatomic, retain) UIButton *searchBar;
@property (nonatomic, retain) AlbumTracks *tracks;
@property (nonatomic, retain) NSMutableArray *categoriesArr;
@property (nonatomic, retain) NSMutableArray *soundsArr;

@property (nonatomic, retain) NSString *collect;

@end

@implementation FindViewController


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"uid"];
    [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"push"];
    [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"return"];
    [_collect release];
    [_searchBar release];
    [_recommendArr release];
    [_moduleTitleArr release];
    [_maybeLikeUserArr release];
    [_todayFocusArr release];
    [_tittleArr release];
    [_smallScrollView release];
    [_mainTableView release];
    [_arr release];
    [_searchField release];
    [_imageUrlArr release];
    [_buttonArr release];
    [super dealloc];
}


- (void)viewWillDisappear:(BOOL)animated

{
    
    [super viewWillDisappear:animated];
    
    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
    
    [self.mm_drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    
    
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
        self.imageUrlArr = [NSMutableArray arrayWithObjects:@"15.jpg", @"15.jpg", @"15.jpg", @"15.jpg", @"15.jpg", @"15.jpg", nil];
        self.maybeLikeUserArr = [NSMutableArray array];
        self.todayFocusArr = [NSMutableArray array];
        self.buttonArr = [NSMutableArray array];
        self.tittleArr = [NSMutableArray array];
        self.recommendArr = [NSMutableArray array];
        [self handleData];
        [self lengData];
        self.show = YES;
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(receiveNotification:) name:@"push" object:nil];
        [center addObserver:self selector:@selector(returnNotification:) name:@"return" object:nil];
        NSNotificationCenter *ficationUid = [NSNotificationCenter defaultCenter];

        [ficationUid addObserver:self selector:@selector(ficationUidAction:) name:@"uid" object:nil];
        
        self.collect = @"NO";
    }
    return self;
}

- (void)ficationUidAction:(NSNotification *)noti
{
    
    if ([noti.object isEqualToString:@"YES"]) {
        
        self.collect = @"NO";
        
        
    }else{
    
    DataBaseHandler *handler = [DataBaseHandler shareInstance];
    
    [handler openDB:[noti.userInfo objectForKey:@"uid"]];
    
    [handler createTable];
    
    self.collect = @"YES";
    }
    NSLog(@"collect = %@", self.collect);
}

- (void)returnNotification:(NSNotification *)noti
{
    self.show = NO;
}

- (void)receiveNotification:(NSNotification *)noti
{
    static FindClassifyViewController *findClassify = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        findClassify = [[FindClassifyViewController alloc] init];
        findClassify.classify = noti.object;
        [self.navigationController pushViewController:findClassify animated:YES];
    });
    findClassify.classify = noti.object;
    findClassify.title = findClassify.classify.title;
    [findClassify.classifyArr removeAllObjects];
    [findClassify handleData];
    [findClassify.collectionView reloadData];
    findClassify.collect =  self.collect;
    NSLog(@"%d", self.isShow);
    if (self.isShow == NO) {
        [self.navigationController pushViewController:findClassify animated:YES];
        
    }
    self.show = YES;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [FilesDownManage sharedFilesDownManageWithBasepath:@"Download" TargetPathArr:[NSArray arrayWithObject:@"DownLoad/mp3"]];
    // 隐藏导航控制器
//    [super.navigationController setNavigationBarHidden:YES];
//    self.automaticallyAdjustsScrollViewInsets = NO;
    
//    [FilesDownManage sharedFilesDownManageWithBasepath:@"315216" TargetPathArr:[NSArray arrayWithObject:@"DownLoad/mp3"]];
    
    self.smallScrollView = [[[EScrollerView alloc] initWithFrameRect:CGRectMake(0, 0, self.view.bounds.size.width, 160) ImageArray:self.imageUrlArr TitleArray:nil] autorelease];
    self.smallScrollView.delegate = self;
    
    self.mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height + 49) style:UITableViewStyleGrouped];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.tableHeaderView = self.smallScrollView;
    [self.view addSubview:self.mainTableView];
    self.mainTableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
    [_mainTableView release];
    
    // navgationBar
    [self setupLeftMenuButton];
    [self setupRightMenuButton];
    
    // searchBar
    [self setupSearchBar];
    

    [self.mainTableView registerClass:[TittleCell class] forCellReuseIdentifier:kReuse];
    [self setupRefresh];
}

- (void)setupSearchBar
{
    self.searchBar = [UIButton buttonWithType:UIButtonTypeCustom];
    self.searchBar.frame = CGRectMake(0, 0, self.view.bounds.size.width / 2, 30);
    self.searchBar.backgroundColor = [UIColor whiteColor];
    [self.searchBar setTitle:@" 搜索声音、专辑、人" forState:UIControlStateNormal];
    [self.searchBar setImage:[UIImage imageNamed:@"iconfont-fangdajing-3.png"] forState:UIControlStateNormal];
    self.searchBar.titleLabel.font = [UIFont systemFontOfSize:14];
    self.searchBar.layer.cornerRadius = 8;
    [self.searchBar setTitleColor:[UIColor colorWithRed:148 / 255.0 green:146 / 255.0 blue:147 / 255.0 alpha:1] forState:UIControlStateNormal];
    [self.searchBar addTarget:self action:@selector(searchBarAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = self.searchBar;
    [_searchBar release];
}

- (void)searchBarAction:(UIButton *)button
{
    SearchViewController *search = [[SearchViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:search];
    [self presentViewController:nav animated:NO completion:^{
        
    }];

}


- (void)setupRightMenuButton
{
    MMDrawerBarButtonItem * rightDrawerButton = [[MMDrawerBarButtonItem alloc] initWithTarget:self action:@selector(rightDrawerButtonPress)];
    [self.navigationItem setRightBarButtonItem:rightDrawerButton animated:YES];
}

// 设置navigation的左按钮为抽屉开关
- (void)setupLeftMenuButton
{
    MMDrawerBarButtonItem * leftDrawerButton = [[MMDrawerBarButtonItem alloc] initWithTarget:self action:@selector(leftDrawerButtonPress:)];
    [self.navigationItem setLeftBarButtonItem:leftDrawerButton animated:YES];
}
// 触发的方法
- (void)leftDrawerButtonPress:(id)sender{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}

- (void)rightDrawerButtonPress
{
    PlayViewController *playVC = [PlayViewController defaultPlayVC];
    [self presentViewController:playVC animated:YES completion:^{
        
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    self.navigationController.navigationBar.translucent = YES;
    [self handleData];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    
    [self.mm_drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
}

- (void)EScrollerViewDidClicked:(NSUInteger)index
{
    FindImageScroll *findImage = [self.arr objectAtIndex:index - 1];
    if ([[findImage.type description] isEqualToString:@"2"]) {
        
        AlbumViewController *albumVC = [[AlbumViewController alloc] init];
        
        ClassifyDetail *classifyDetail = [[ClassifyDetail alloc] init];
        
        classifyDetail.pageId = findImage.albumId;
        
        albumVC.classifyDetail = classifyDetail;
        
        [self.navigationController pushViewController:albumVC animated:YES];
        
        [classifyDetail release];
        [albumVC release];
        
    }else if ([[findImage.type description] isEqualToString:@"3"]){
        
        PlayViewController *playVC = [PlayViewController defaultPlayVC];
        
        if (playVC.albumArr.count != 0) {
            [playVC.albumArr removeAllObjects];
            playVC.index = 0;
        }
        if (playVC.hotPlayArr.count != 0) {
            playVC.index = 0;
            [playVC.hotPlayArr removeAllObjects];
        }
        if (playVC.focusFetailArr.count != 0) {
            playVC.index = 0;
            [playVC.focusFetailArr removeAllObjects];
        }
        Album *album = [[Album alloc] init];
        album.albumId = findImage.trackId;
        playVC.albumPlay = album;
        
        [self presentViewController:playVC animated:YES completion:^{
            
            
        }];
        
        
    }
//    NSLog(@"图片--%ld",index);
}

- (void)settingAction:(UIButton *)button
{
    NSLog(@"点击设置");
}

- (void)lengData
{
    NSString *str = kFindList2API;
    
    [NetHandler getDataWithUrl:str completion:^(NSData *data) {
        
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *array = [result objectForKey:@"categories"];
        
        self.categoriesArr = [NSMutableArray array];
        
        for (NSDictionary *dic in array) {
            
            CategoriesNew *categor = [[CategoriesNew alloc] initWithDictionary:dic];
            
            [self.categoriesArr addObject:categor];
            
            [categor release];
            
            for (NSDictionary *dic in categor.sounds) {
                
                ComprehensiveAlbum *compre = [[ComprehensiveAlbum alloc] initWithDictionary:dic];
                
                [self.soundsArr addObject:compre];
                
                [compre release];
                
            }
            
        }
    }];
    
}

- (void)handleData
{
    NSString *str = kFindListAPI;
    [NetHandler getDataWithUrl:str completion:^(NSData *data) {
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        // tableView的头滚动视图
        NSDictionary *dic = [result objectForKey:@"focusImages"];
        [self.imageUrlArr removeAllObjects];
        NSArray *array = [dic objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            FindImageScroll *fousImage = [[FindImageScroll alloc] initWithDictionary:dic];
            [self.arr addObject:fousImage];
//            NSLog(@"%@", fousImage);
            [self.imageUrlArr addObject:fousImage.pic];
            [self.tittleArr  addObject:fousImage.shortTitle];
            [fousImage release];
        }
        // 19个按钮的数据获取
        NSDictionary *buttonDic = [result objectForKey:@"categories"];
        NSArray *buttonArr = [buttonDic objectForKey:@"data"];
        if (self.buttonArr.count != 0) {
            [self.buttonArr removeAllObjects];
        }
        for (NSDictionary *dic in buttonArr) {
            Classify *classify = [[Classify alloc] initWithDictionary:dic];
            [self.buttonArr addObject:classify];
            [classify release];
        }
        NSDictionary *findData = [NSDictionary dictionaryWithObjectsAndKeys:self.buttonArr, @"array", nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"findData" object:nil userInfo:findData];
        // 今日焦点
        NSDictionary *todayFocusDic = [result objectForKey:@"subjects"];
        NSString *moduleTitle = [todayFocusDic objectForKey:@"moduleTitle"];
        
        NSArray *listArr = [todayFocusDic objectForKey:@"list"];
        if (self.todayFocusArr.count != 0) {
            [self.todayFocusArr removeAllObjects];
        }
        for (NSDictionary *dic in listArr) {
            FindTodayFocus *findTodayFocus = [[FindTodayFocus alloc] initWithDictionary:dic];
            findTodayFocus.moduleTitle = moduleTitle;
            [self.todayFocusArr addObject:findTodayFocus];
            [findTodayFocus release];
        }
        
        // 最火播主
        NSArray *maybeLikeUserArr = [result objectForKey:@"maybeLikeUsers"];
        if (self.maybeLikeUserArr.count != 0) {
            [self.maybeLikeUserArr removeAllObjects];
        }
        for (NSDictionary *dic in maybeLikeUserArr) {
            FindMayBeLikeUser *findMayBeLikeUser = [[FindMayBeLikeUser alloc] initWithDictionary:dic];
            [self.maybeLikeUserArr addObject:findMayBeLikeUser];
            [findMayBeLikeUser release];
        }
        // 推荐专辑
        NSDictionary *recommendDic = [result objectForKey:@"recommendAlbums"];
        NSArray *recommendArr = [recommendDic objectForKey:@"list"];
        if (self.recommendArr.count != 0) {
            [self.recommendArr removeAllObjects];
        }
        for (NSDictionary *dic in recommendArr) {
            Recommend *recommend = [[Recommend alloc] initWithDictionary:dic];
            [self.recommendArr addObject:recommend];
            [recommend release];
        }
        self.moduleTitleArr = [NSMutableArray arrayWithObjects:@"今日焦点", @"推荐专辑", @"最火播主", nil];
        [self.mainTableView reloadData];
        // 重新刷新tableView的头
        EScrollerView *scrollerView = [[[EScrollerView alloc] initWithFrameRect:CGRectMake(0, 0, self.view.bounds.size.width, 160) ImageArray:self.imageUrlArr TitleArray:nil] autorelease];
        scrollerView.delegate = self;
        if (self.smallScrollView != scrollerView) {
            [_smallScrollView release];
            _smallScrollView = [scrollerView retain];
        }
        self.mainTableView.tableHeaderView = self.smallScrollView;
        
    }];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.moduleTitleArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 0:
        {
//            NSLog(@"%@", [self.moduleTitleArr objectAtIndex:0]);
            
            FocusMoreViewController *moreVC = [[FocusMoreViewController alloc] init];
            
            [self.navigationController pushViewController:moreVC animated:YES];
            [moreVC release];
        }
            break;
        case 1:
            {
                FindClassifyDetailViewController *findDetailVC = [[FindClassifyDetailViewController alloc] init];
                
                Classify *classify = [[Classify alloc] init];
                classify.name = @"all";
                classify.tname = [self.moduleTitleArr objectAtIndex:1];
                
                findDetailVC.classify = classify;
                findDetailVC.collect = self.collect;
                [self.navigationController pushViewController:findDetailVC animated:YES];
                
                [findDetailVC release];
            }
            break;
        case 2:
            NSLog(@"%@", [self.moduleTitleArr objectAtIndex:2]);
            TittleCell *tittle = (TittleCell *)[tableView cellForRowAtIndexPath:indexPath];
            [tittle.myButton addTarget:self action:@selector(pushHotPresenter) forControlEvents:UIControlEventTouchUpInside];
            [self pushHotPresenter];
            break;
        default:
            break;
    }
    
}

- (void)pushHotPresenter
{
    FindHotPresenterViewController *find = [[FindHotPresenterViewController alloc] init];
    [self.navigationController pushViewController:find animated:YES];
}

- (void)mayBeLikeUserAction:(UIButton *)button
{
    HotPresenterHomepageController *hot = [[HotPresenterHomepageController alloc] init];
    hot.mayBeLikeUser = [self.maybeLikeUserArr objectAtIndex:button.tag];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:hot animated:YES];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return (self.view.bounds.size.height / 2);
    }
    else if (section == 2) {
        return (self.view.bounds.size.height / 4.2);
    }
    else if (section == 1) {
        return (self.view.bounds.size.height / 3 * 2);
    }
    return 50;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return [self.moduleTitleArr objectAtIndex:section];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *footerView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 2)] autorelease];
        footerView.backgroundColor = [UIColor whiteColor];
        // 大图button
        UIButton *todayFocusFirstButton = [UIButton buttonWithType:UIButtonTypeCustom];
        // 设置大图的大小
        todayFocusFirstButton.frame = CGRectMake(10, 0, self.view.bounds.size.width - 20, footerView.bounds.size.height / 2);
        // 从数组中取出大图信息
        FindTodayFocus *todayFocus = [self.todayFocusArr objectAtIndex:0];
        // 设置大图的tag值
        todayFocusFirstButton.tag = kTodayFocusTag;
        // 给大图添加点击事件
        [todayFocusFirstButton addTarget:self action:@selector(todayFocusAction:) forControlEvents:UIControlEventTouchUpInside];
        // 给大图添加图片
        [todayFocusFirstButton setImageWithURL:[NSURL URLWithString:[todayFocus coverPathBig]]];
        // footerView添加按钮
        [footerView addSubview:todayFocusFirstButton];
        // 给大图添加一个文字说明
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, todayFocusFirstButton.bounds.size.height / 6 * 5 - 5, todayFocusFirstButton.bounds.size.width - 10, todayFocusFirstButton.bounds.size.height / 6 - 5)];
        // 设置文字说明内容
        label.text = todayFocus.title;
        // 设置文字大小
        label.font = [UIFont boldSystemFontOfSize:15];
        // 设置文字颜色
        label.textColor = [UIColor whiteColor];
        // 给按钮添加文字说明
        [todayFocusFirstButton addSubview:label];
        
        for (int i = 1; i < 3; i++) {
            // 创建一个按钮
            UIButton *todayFocusButton = [UIButton buttonWithType:UIButtonTypeCustom];
            // 设置按钮的tag值
            todayFocusButton.tag = kTodayFocusTag + i;
            // 设置按钮的大小
            todayFocusButton.frame = CGRectMake(10, footerView.bounds.size.height / 2 + ((i - 1) * footerView.bounds.size.height / 4), footerView.bounds.size.width - 20, footerView.bounds.size.height / 4);
            // 给按钮添加点击事件
            [todayFocusButton addTarget:self action:@selector(todayFocusAction:) forControlEvents:UIControlEventTouchUpInside];
            // 从数组中取出想要的数据
            FindTodayFocus *todayFocus = [self.todayFocusArr objectAtIndex:i];
            // 设置按钮的标题
            [todayFocusButton setTitle:todayFocus.title forState:UIControlStateNormal];
            // 让按钮上的文字居左显示
            todayFocusButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            todayFocusButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
            // 设置按钮上文字大小
            todayFocusButton.titleLabel.font = [UIFont systemFontOfSize:15];
            // 设置按钮上普通状态下文字的颜色为黑色
            [todayFocusButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            // 设置按钮上点击状态下文字的颜色为橘黄色
            [todayFocusButton setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
            // 按钮上图片的大小与位置
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(todayFocusButton.bounds.size.width - (todayFocusButton.bounds.size.height - 20), 10, todayFocusButton.bounds.size.height - 20, todayFocusButton.bounds.size.height - 20)];
            // 给按钮设置小图
            [imageView setImageWithURL:[NSURL URLWithString:todayFocus.coverPathSmall]];
            // 在按钮上添加imageView
            [todayFocusButton addSubview:imageView];
            // 在cell的footerView上添加该按钮
            [footerView addSubview:todayFocusButton];
        }
        
        return footerView;
    } else if (section == 2) {
        UIView *footerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 4.2)] autorelease];
        for (int i = 0; i < 3; i++) {
            //        self.backgroundColor = [UIColor blueColor];
            // 3个最火播主的图片按钮
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.backgroundColor = [UIColor whiteColor];
            FindMayBeLikeUser *find = [_maybeLikeUserArr objectAtIndex:i];
            [button setImageWithURL:[NSURL URLWithString:[find largeLogo]]];
            button.tag = kMayBeLikeUserTag + i;
            
            // UIApplication的宽度, 觉得按钮的宽度
            button.frame = CGRectMake(i * (([UIApplication sharedApplication].keyWindow.bounds.size.width - 10) / 3) + 10, 0, ([UIApplication sharedApplication].keyWindow.bounds.size.width - 10) / 3 - 10, ([UIApplication sharedApplication].keyWindow.bounds.size.width - 10) / 3 - 10);
            [button addTarget:self action:@selector(mayBeLikeUserAction:) forControlEvents:UIControlEventTouchUpInside];
            [footerView addSubview:button];
            
            // 3个最火播主的图片标题
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i * (([UIApplication sharedApplication].keyWindow.bounds.size.width - 10) / 3) + 10, ([UIApplication sharedApplication].keyWindow.bounds.size.width - 10) / 3 - 10, ([UIApplication sharedApplication].keyWindow.bounds.size.width - 10) / 3 - 10, [UIApplication sharedApplication].keyWindow.bounds.size.height / 4.2 - ((self.view.bounds.size.width - 10) / 3 - 10))];
            label.text = find.nickname;
            label.font = [UIFont systemFontOfSize:14];
            label.textAlignment = NSTextAlignmentCenter;
            [footerView addSubview:label];
            [label release];
        }
        return footerView;
    } else if (section == 1) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake((self.view.bounds.size.width - 30) / 2, (self.view.bounds.size.height / 3 * 2 - 20) / 2);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 3 * 2) collectionViewLayout:flowLayout];
        collectionView.scrollEnabled = NO;
        collectionView.dataSource = self;
        collectionView.delegate = self;
        collectionView.backgroundColor = [UIColor whiteColor];
        [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionReuse];
        return collectionView;
    }
    
    return nil;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.recommendArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionReuse forIndexPath:indexPath];
    UIImageView *imageSmallView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, cell.contentView.bounds.size.width - 10, cell.contentView.bounds.size.width - 10)];
    Recommend *recommend = [self.recommendArr objectAtIndex:indexPath.row];
    [imageSmallView setImageWithURL:[NSURL URLWithString:recommend.albumCoverUrl290]];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5 + (cell.contentView.bounds.size.width - 10), imageSmallView.bounds.size.width, cell.contentView.bounds.size.height - 5 - (cell.contentView.bounds.size.width - 20))];
    label.font = [UIFont systemFontOfSize:13];
    label.text = recommend.title;
    label.textAlignment = NSTextAlignmentCenter;
    [cell.contentView addSubview:label];
    [cell.contentView addSubview:imageSmallView];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AlbumViewController *albumVC = [[AlbumViewController alloc] init];
    
    Recommend *recommend = [self.recommendArr objectAtIndex:indexPath.row];
    
    ClassifyDetail *classifyDeta = [[ClassifyDetail alloc] init];
    
    classifyDeta.pageId = recommend.recommendId;
    classifyDeta.title = recommend.title;
    
    albumVC.classifyDetail = classifyDeta;
    
    
    [self.navigationController pushViewController:albumVC animated:YES];
    
    [albumVC release];
    [classifyDeta release];
}

- (void)todayFocusAction:(UIButton *)button
{
//    NSLog(@"%ld", button.tag);
    FoThreeViewController *focusVC = [[FoThreeViewController alloc] init];
    
    [self.navigationController pushViewController:focusVC animated:YES];
    
    int i = 0;
    if (button.tag == 100) {
        i = 0;
    } else if (button.tag == 101) {
        i = 1;
    } else {
        i = 2;
    }
    
    focusVC.focus = [self.todayFocusArr objectAtIndex:i];
    
    [focusVC release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TittleCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuse];
    cell.myLabel.text = [self.moduleTitleArr objectAtIndex:indexPath.section];
    cell.myImageView.image = [UIImage imageNamed:@"iconfont-icon.png"];
    [cell.myButton setTitle:@"更多>>" forState:UIControlStateNormal];
    [cell.myButton setTitleColor:[UIColor kuGouColor] forState:UIControlStateNormal];
    cell.myButton.userInteractionEnabled = NO;
    cell.myButton.titleLabel.font = [UIFont systemFontOfSize:15];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    NSLog(@"调用清理内存的方法");
    [[SDImageCache sharedImageCache] clearMemory];
    
}



- (void)setupRefresh
{
    //    NSLog(@"进入了setupRefresh");
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    
    [self.mainTableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    //进入刷新状态(一进入程序就下拉刷新)
    [self.mainTableView headerBeginRefreshing];
    
    
//    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
//    [self.mainTableView addFooterWithTarget:self action:@selector(footerRereshing)];
}



#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    
    //1. 在这调用请求网络数据方法（刷新数据）
    
    [self handleData];
    
    
    //2. 2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.mainTableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.mainTableView headerEndRefreshing];
    });
}

//   进入加载状态
- (void)footerRereshing
{
    //1. 拼接口等操作
    // 请求加载数据
    
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.mainTableView reloadData];
        [self.mainTableView footerEndRefreshing];
    });
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
